package cn.mumzone.pos.ui.fragment.inventory;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.mumzone.basic.base.BaseFragment;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.InventoryItemEntity;
import cn.mumzone.pos.presenter.InventoryItemPresenter;
import cn.mumzone.pos.presenter.contract.InventoryItemContract;
import cn.mumzone.pos.ui.activity.ACT_InventoryDetail;
import cn.mumzone.pos.ui.adapter.inventory.ADA_InventoryItem;
import cn.mumzone.pos.ui.fragment.bills.FRA_BillItem;
import cn.mumzone.pos.ui.fragment.warehouse.FRA_WarehouseItem;
import cn.mumzone.pos.ui.widgets.refresh.SmartRefreshLayout;
import cn.mumzone.pos.ui.widgets.refresh.api.RefreshLayout;
import cn.mumzone.pos.ui.widgets.refresh.constant.RefreshState;
import cn.mumzone.pos.ui.widgets.refresh.listener.OnRefreshListener;
import cn.mumzone.pos.util.DateHolder;

/**
 * Created By Dlx
 * on 2019/9/25
 */
public class FRA_InventoryItem extends BaseFragment<InventoryItemPresenter> implements InventoryItemContract.View, ADA_InventoryItem.OnCardClickListener, OnRefreshListener, OnLoadMoreListener {
    public static final int TYPE_ALL = 100;
    public static final int TYPE_NOT_STARTED = 101;
    public static final int TYPE_INVENTORYING = 102;
    public static final int TYPE_FINISH = 103;
    @BindView(R.id.billitem_recyclerview)
    LRecyclerView recyclerview;
    @BindView(R.id.ll_bill_list)
    LinearLayout loadingLayout;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    private int type;
    private int pageNum = 1;

    private ADA_InventoryItem adapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private boolean isVisible = false;
    private boolean isFirst = true;
    private boolean isRefreshing = false;

    public static FRA_InventoryItem newInstance(int type) {
        FRA_InventoryItem fra = new FRA_InventoryItem();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fra.setArguments(bundle);
        return fra;
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isFirst) {
            isFirst = false;
            return;
        }
        if (isVisibleToUser) {
            isVisible = true;
            if (mPresenter == null)
                return;
            refreshLayout.autoRefresh();
        } else {
            isVisible = false;
        }

    }

    @Override
    protected void onUserVisible() {
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        type = extras.getInt("type", -1);
//        dateTime = System.currentTimeMillis();
    }

    @Override
    protected void initEventAndData() {

        mPresenter.initEvent();
//        mPresenter.getDatas(pageNum, "", getInventoryStatus(),
//                AppDateUtil.getTimeStamp(DateHolder.INSTANCE.getInventoryDate(), AppDateUtil.YYYY_MM_DD),
//                AppDateUtil.getTimeStamp(DateHolder.INSTANCE.getInventoryDate(), AppDateUtil.YYYY_MM_DD), DateHolder.INSTANCE.getInventroyKey());

        final GridLayoutManager manager = new GridLayoutManager(mContext, 4);
        recyclerview.setLayoutManager(manager);

        adapter = new ADA_InventoryItem(mContext);
        adapter.setOnCardClickListener(this);

        lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        recyclerview.setAdapter(lRecyclerViewAdapter);

        recyclerview.setPullRefreshEnabled(false);

        refreshLayout.setOnRefreshListener(this);

        recyclerview.setOnLoadMoreListener(this);

        refreshLayout.autoRefresh();
    }

    private String getInventoryStatus() {
        if (type == TYPE_ALL)  // 全部
            return "";
        if (type == TYPE_NOT_STARTED)  //未开始
            return "0";
        if (type == TYPE_INVENTORYING)  //进行中包括 进行中，审核中
            return "5";
        if (type == TYPE_FINISH)  //结束
            return "3";
        return "";
    }

    @Override
    protected int getLayout() {
        return R.layout.fra_inventory_item;
    }

    @Override
    protected View getTargetView() {
        return loadingLayout;
    }

    @Override
    public void showToast(String str) {

    }

    @Override
    public void showError(String msg) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        toggleShowEmpty(true, R.mipmap.ic_bill_empty, "暂无盘点单", "");
    }


    @Override
    public void showNetError() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        showNetErrorEmpty(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FRA_InventoryItem.super.toggleCloseEmpty();
                refreshLayout.autoRefresh();
            }
        });
    }

    @Override
    public void onDataReceived(PageListResponse<InventoryItemEntity> response) {

        toggleShowEmpty(false);
        adapter.update(response.getList(), pageNum == 1);
        lRecyclerViewAdapter.notifyDataSetChanged();
        refreshLayout.finishRefresh();
    }

    @Override
    public void noMore() {
        recyclerview.setNoMore(true);
    }

    @Override
    public void noData() {
        refreshLayout.finishRefresh();
        toggleShowEmpty(true, R.mipmap.ic_bill_empty, "暂无盘点单", "你可以换个日期试试");
    }

    @Override
    public void searchFailed() {
        refreshLayout.finishRefresh();
        toggleShowEmpty(true, R.mipmap.ic_bill_empty, "没有找到任何内容", "你可以换个关键词试试~");
    }

    @Override
    public void onDateChanged(long date) {
        //时间切换，所有子页面修改时间 重新请求
        if (!isVisible)
            return;
        pageNum = 1;
        recyclerview.setNoMore(false);
//        mPresenter.getDatas(pageNum, "", getInventoryStatus(),
//                AppDateUtil.getTimeStamp(DateHolder.INSTANCE.getInventoryDate(), AppDateUtil.YYYY_MM_DD),
//                AppDateUtil.getTimeStamp(DateHolder.INSTANCE.getInventoryDate(), AppDateUtil.YYYY_MM_DD), DateHolder.INSTANCE.getInventroyKey());
        refreshLayout.autoRefresh();
    }

    @Override
    public void onSearchKeyCome(String key) {
        //搜索，所有页面进行搜索
        if (!isVisible)
            return;
        pageNum = 1;
        recyclerview.setNoMore(false);
//        mPresenter.getDatas(pageNum, "", getInventoryStatus(),
//                "",
//                "", DateHolder.INSTANCE.getInventroyKey());
        refreshLayout.autoRefresh();
    }

    @Override
    public void onCardClick(String id, String inventoryStatus) {
        //列表中条目被点击的事件  进行中弹出提示，不跳转
        if ("5".equals(inventoryStatus)) {
            PosToast.INSTANCE.makeShortToast(mContext, "盘点中，无法查看单据~");
            return;
        }
        //todo 跳转到盘点单详情操作
        ACT_InventoryDetail.startAc(mContext, id);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        recyclerview.setNoMore(false);
        mPresenter.getDatas(pageNum, "", getInventoryStatus(),
                StringUtils.isEmpty(DateHolder.INSTANCE.getInventroyKey()) ? AppDateUtil.getTimeStamp(DateHolder.INSTANCE.getInventoryDate(), AppDateUtil.YYYY_MM_DD) : "",
                StringUtils.isEmpty(DateHolder.INSTANCE.getInventroyKey()) ? AppDateUtil.getTimeStamp(DateHolder.INSTANCE.getInventoryDate(), AppDateUtil.YYYY_MM_DD) : "", DateHolder.INSTANCE.getInventroyKey());
    }

    @Override
    public void onLoadMore() {
        pageNum++;
        mPresenter.getDatas(pageNum, "", getInventoryStatus(),
                AppDateUtil.getTimeStamp(DateHolder.INSTANCE.getInventoryDate(), AppDateUtil.YYYY_MM_DD),
                AppDateUtil.getTimeStamp(DateHolder.INSTANCE.getInventoryDate(), AppDateUtil.YYYY_MM_DD), DateHolder.INSTANCE.getInventroyKey());
    }
    public boolean isRefreshing() {
        return refreshLayout.getState() != RefreshState.None;
    }
}
